package api

import (
	"fmt"
	"template/generation/generation"

	"github.com/spf13/cobra"
)

var (
	engineering string
	port        string
	namespace   string
	directory   string // New variable for directory
	giturl      string
	env         string
	StartCmd    = &cobra.Command{
		Use:     "create",
		Short:   "配置文件生成器",
		Example: "configctl create -e [proj name] -p [proj port] -n [proj namespace] -d [directory] -g [git url] -v [env]",
		Run: func(cmd *cobra.Command, args []string) {
			if engineering == "" || namespace == "" || port == "" || giturl == "" || env == "" {
				cmd.Help()
				return
			}
			if env == "all" || env == "dev" || env == "pre" || env == "prod" {
				ad := generation.AutoCodeService{}
				str, err := ad.CreatePackageTemp(engineering, port, namespace, directory, giturl, env) // Modify this line as needed
				if err != nil {
					panic(err)
				}
				fmt.Println(str)
				return
			}
			fmt.Println("请指定正确的部署环境")

		},
	}
)

func init() {
	// 第一参数位为：为go定义的参数变量
	// 第二参数位为：为外部完整参数，如：configcil --engineering test
	// 第三参数位为：为简化参数，如:configcil -e test
	// 第四参数位为：为默认参数变量，如果命令行不写-e参数的话，就取默认值
	// 第五参数位为：为注解，会写道help里面
	StartCmd.PersistentFlags().StringVarP(&engineering, "engineering", "e", "", "[输入要生成的项目名称]")
	StartCmd.PersistentFlags().StringVarP(&port, "port", "p", "", "[输入项目端口号]")
	StartCmd.PersistentFlags().StringVarP(&namespace, "namespace", "n", "", "[K8s命名空间名称]")
	StartCmd.PersistentFlags().StringVarP(&directory, "directory", "d", "", "[指定目录路径]") // Registering the directory flag
	StartCmd.PersistentFlags().StringVarP(&giturl, "giturl", "g", "", "[指定git地址]")      // Registering the giturl flag
	StartCmd.PersistentFlags().StringVarP(&env, "env", "v", "", "[指定环境,全部指定all]")       // Registering the env

}
